package org.shock.ss.sms.action.bbtj;

import java.util.List;

import org.shock.ss.sms.action.BaseAction;
import org.shock.ss.sms.bean.app.CustomerInfo;
import org.shock.ss.sms.bean.app.InRecords;
import org.shock.ss.sms.bean.app.OutRecords;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.dao.app.impl.InRecordsDaoImpl;
import org.shock.ss.sms.service.app.InRecordsService;
import org.shock.ss.sms.service.app.impl.InRecordsServiceImpl;

import com.alibaba.fastjson.JSON;

public class BbtjAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private InRecordsService cs = new InRecordsServiceImpl((InRecordsDaoImpl) DaoFactory.getDao("InRecordsDao"));
	private List list;
	private InRecords inRecords = new InRecords();
	private String phone;

	public String customerArea() {
		return SUCCESS;
	}

	public String kctj() {
		return SUCCESS;
	}

	public String customerSelf() throws Exception {
		if (Util.isEmpty(inRecords.getProductcode()) && Util.isEmpty(inRecords.getProductcode()) && Util.isEmpty(phone)) {
			sqladd.append(" and a.productcode=0");
		}
		if (!Util.isEmpty(inRecords.getProductcode())) {
			sqladd.append(" and a.productcode='" + inRecords.getProductcode() + "'");
		}
		if (!Util.isEmpty(inRecords.getOrdercode())) {
			sqladd.append(" and ordercode='" + inRecords.getOrdercode() + "'");
		}
		if (!Util.isEmpty(phone)) {
			sqladd.append(" and customerid=(select id from " + CustomerInfo.getTable() + " where phone='" + phone
					+ "')");
		}
		String sql = "select max(outtime) as outtime, max(isck) as isck, a.productcode,ordercode,customerid,CONCAT(c.name,c.phone,c.province,c.city) as customername,sum(cargonum) as cargonum,max(intime) as intime from "
				+ CustomerInfo.getTable()
				+ " c,"
				+ InRecords.getTable()
				+ " a left join "
				+ OutRecords.getTable()
				+ " o on a.productcode=o.productcode where a.customerid=c.id"
				+ sqladd
				+ " group by a.productcode,ordercode,customerid,c.name";
		String sqlCount = "select count(distinct productcode) from " + InRecords.getTable() + "  a where 1=1" + sqladd;
		if (pageno == 1) {
			pageno = (start / rownum) + 1;
		}
		pager = cs.list(dir, "a." + sort, pageno, rownum, sqlCount, sql);
		totalCount = pager.getTotalCount();
		list = pager.getList();
		jsonString = JSON.toJSONString(pager);
		System.out.println(jsonString);
		return SUCCESS;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public InRecords getInRecords() {
		return inRecords;
	}

	public void setInRecords(InRecords inRecords) {
		this.inRecords = inRecords;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
}
